<route lang="yaml">
meta:
enabled: false
</route>

<script setup lang="ts">
import api from '@/api/modules/carousel'
import { ElMessage, ElMessageBox } from 'element-plus'
import { watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

defineOptions({
  name: 'PagesExampleMenuList',
})
const { pagination, getParams, onSizeChange, onCurrentChange } = usePagination()

const router = useRouter()

// 表格是否自适应高度
const tableAutoHeight = ref(false)

// 列表
const loading = ref(false)
const dataList = ref([])
const route = useRoute()

watch(
  () => route.query.timestamp,
  () => {
    if (route.name === 'PagesExampleMenuList') {
      getDataList()
    }
  },
)
onMounted(() => {
  getDataList()
})

// 初始化
function getDataList() {
  loading.value = true
  const params = {
    ...(getParams()),
  }
  api.list(params).then((res: any) => {
    loading.value = false
    dataList.value = res.data
    pagination.value.total = res.total
  })
}

// 新增
function onCreate(row?: any) {
  router.push({
    name: 'carouselCreate',
    query: {
      parentId: row.id,
    },
  })
}

function onDel(row: any) {
  ElMessageBox.confirm(`确认删除「轮播图」吗？`, '确认信息').then(() => {
    api.delete(Number(row.carouselId)).then((res: any) => {
      if (res.code === 200) {
        getDataList()
        ElMessage.success({
          message: '删除成功',
        })
      }
    })
  }).catch(() => {})
}
// 每页数量切换
function sizeChange(size: number) {
  onSizeChange(size).then(() => getDataList())
}

// 当前页码切换（翻页）
function currentChange(page = 1) {
  onCurrentChange(page).then(() => getDataList())
}

// 照片预览
function previewImage(url: string) {
  window.open(url, '_blank')
}
</script>

<template>
  <div :class="{ 'absolute-container': tableAutoHeight }">
    <FaPageHeader title="轮播图管理" class="mb-0" />
    <FaPageMain :class="{ 'flex-1 overflow-auto': tableAutoHeight }" :main-class="{ 'flex-1 flex flex-col overflow-auto': tableAutoHeight }">
      <ElDivider border-style="dashed" />
      <FaPageMain :class="{ 'flex-1 overflow-auto': tableAutoHeight }" :main-class="{ 'flex-1 flex flex-col overflow-auto': tableAutoHeight }">
        <ElSpace>
          <ElButton v-auth="'system:carousel:add'" type="primary" size="default" @click="onCreate">
            <template #icon>
              <FaIcon name="i-ep:plus" />
            </template>
            新增轮播图
          </ElButton>
        </ElSpace>
        <ElTable v-loading="loading" class="mt-4" :data="dataList" row-key="id" stripe highlight-current-row default-expand-all border height="100%">
          <ElTableColumn label="轮播图">
            <template #default="scope">
              <div class="table-image-preview">
                <img :src="scope.row.carouselUrl" alt="轮播图" style="max-height: 100px;" @click="previewImage(scope.row.themeUrl)">
              </div>
            </template>
          </ElTableColumn>
          <ElTableColumn prop="carouselDescribe" label="中文简介" />
          <ElTableColumn prop="carouselDescribeEn" label="英文简介" />
          <ElTableColumn label="操作" width="200" align="center" fixed="right">
            <template #default="scope">
              <ElSpace>
                <ElButton v-auth="'system:carousel:del'" type="primary" size="small" plain @click="onDel(scope.row)">
                  删除
                </ElButton>
              </ElSpace>
            </template>
          </ElTableColumn>
        </ElTable>
        <ElPagination style="margin-top: 20px" :current-page="pagination.page" :total="pagination.total" :page-size="pagination.size" :page-sizes="pagination.sizes" :layout="pagination.layout" :hide-on-single-page="false" class="pagination" background @current-change="currentChange" @size-change="sizeChange" />
      </FaPageMain>
    </fapagemain>
  </div>
</template>

<style scoped>
.absolute-container {
  position: absolute;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}
</style>
